define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template', 'echarts', 'echarts-theme'], function ($, undefined, Backend, Table, Form, Template, Echarts) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'weblog/log/index' + location.search,
                    del_url: 'weblog/log/del',
                    table: 'weblog_log',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false,sortable: true},
                        {field: 'weblogsite.name', title: __('Weblogsite.name'), searchList: function (column) {
                            return Template('site_tpl', {});
                        }},
                        {field: 'weblogspider.name', title: __('Weblogspider.name'), searchList: function (column) {
                            return Template('spider_tpl', {});
                        }},
                        {field: 'ip', title: __('Ip'), events: Table.api.events.ip, formatter: Table.api.formatter.search},
                        {field: 'sign', title: __('Sign'), cellStyle: Controller.api.formatTableUnit, formatter: Controller.api.paramsMatter, operate:false},
                        {field: 'username', title: __('Username'), cellStyle: Controller.api.formatTableUnit, formatter: Controller.api.paramsMatter, operate:false},
                        {field: 'datetime', title: __('Datetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'method', title: __('Method')},
                        {field: 'url', title: __('Url'), cellStyle: Controller.api.formatTableUnit, formatter: Controller.api.paramsMatter},
                        {field: 'protocol', title: __('Protocol')},
                        {field: 'code', title: __('Code')},
                        {field: 'size', title: __('Size')},
                        {field: 'referer', title: __('Referer'), formatter: Table.api.formatter.url},
                        {field: 'other_info', title: __('Other_info'), cellStyle: Controller.api.formatTableUnit, formatter: Controller.api.paramsMatter, operate:false},
                        //{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [{
                                name: 'detail',
                                text: __('Detail'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'weblog/log/edit'
                            }],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);


            // 统计按钮
            $(document).on("click", ".btn-start", function () {
                var data = this ? $(this).data() : {};
                Fast.api.open(data.url, $(this).text(), {area: ["80%", "70%"]});
            });
        },
        edit: function () {
            Controller.api.bindevent();
        },
        site: function (){
            Form.api.bindevent($("form[role=form]"), function (data, ret) {
                if (!data.siteData || data.siteData.length < 1) {
                    $('#null_data').show();
                    $('#null_data').text('查无数据');
                    $('.connectedSortable').addClass('hide');
                    return false;
                }
                $('.connectedSortable').removeClass('hide');
                $('#null_data').hide();
                var pieChart1 = Echarts.init(document.getElementById('pie-chart1'), 'walden');
                var pieChart2 = Echarts.init(document.getElementById('pie-chart2'), 'walden');
                var option1 = {
                    tooltip: {
                        trigger: 'item',
                        formatter: '{a} <br/>{b}: {c} ({d}%)'
                    },
                    legend: {
                        selectedMode: true,
                        bottom:0,
                        data: data.siteData
                    },
                    series: [
                        {
                            name: '访问次数',
                            type: 'pie',
                            radius: ['50%', '70%'],
                            avoidLabelOverlap: false,
                            label: {
                                normal: {
                                    show: true,
                                    position: 'left',
                                    formatter: "{b}：{c}（{d}%）",
                                },
                                emphasis: {
                                    show: false,
                                    textStyle: {
                                        fontSize: '30',
                                        fontWeight: 'bold'
                                    }
                                }
                            },
                            normal: {
                                show: false,//是否显示标签
                            },
                            data: data.visitsCount
                        }
                    ]
                };
                var option2 = {
                    tooltip: {
                        trigger: 'item',
                        formatter: '{a} <br/>{b}: {c} ({d}%)'
                    },
                    legend: {
                        selectedMode: true,
                        bottom:0,
                        data: data.siteData
                    },
                    series: [
                        {
                            name: '访问次数',
                            type: 'pie',
                            radius: ['50%', '70%'],
                            avoidLabelOverlap: false,
                            label: {
                                normal: {
                                    show: true,
                                    position: 'left',
                                    formatter: "{b}：{c}（{d}%）",
                                },
                                emphasis: {
                                    show: false,
                                    textStyle: {
                                        fontSize: '30',
                                        fontWeight: 'bold'
                                    }
                                }
                            },
                            normal: {
                                show: false,//是否显示标签
                            },
                            data: data.ipCount
                        }
                    ]
                };
                // 使用刚指定的配置项和数据显示图表。
                pieChart1.setOption(option1);
                pieChart2.setOption(option2);
                return false;
            });
            $("form[role=form]").submit();
        },
        spider: function (){
            Form.api.bindevent($("form[role=form]"), function (data, ret) {
                if (!data.spiderData || data.spiderData.length < 1) {
                    $('#null_data').show();
                    $('#null_data').text('无蜘蛛访问记录');
                    $('.connectedSortable').addClass('hide');
                    return false;
                }
                $('.connectedSortable').removeClass('hide');
                $('#null_data').hide();

                var barChart = Echarts.init(document.getElementById('bar-chart'), 'walden');
                // 指定图表的配置项和数据
                var option = {
                    xAxis: {
                        type: "category",
                        data: data.spiderData,
                        itemStyle: {
                            narmal: {
                                color: "orange"
                            }
                        }
                    },
                    yAxis: {
                        type: "value"
                    },
                    series: [
                        {
                            data: data.visitsCount,
                            type: "bar",
                            barWidth: "70%",
                            itemStyle: {
                                normal: {
                                    //每根柱子颜色设置
                                    color: function(params) {
                                        let colorList = [
                                            "#c23531",
                                            "#2f4554",
                                            "#61a0a8",
                                            "#d48265",
                                            "#91c7ae",
                                            "#749f83",
                                            "#ca8622",
                                            "#bda29a",
                                            "#6e7074",
                                            "#546570",
                                            "#c4ccd3",
                                            "#4BABDE",
                                            "#FFDE76",
                                            "#E43C59",
                                            "#37A2DA",
                                            "#c23531",
                                            "#2f4554",
                                            "#61a0a8",
                                            "#d48265",
                                            "#91c7ae",
                                            "#749f83",
                                            "#ca8622",
                                            "#bda29a",
                                            "#6e7074",
                                            "#546570",
                                            "#c4ccd3",
                                            "#4BABDE",
                                            "#FFDE76",
                                            "#E43C59",
                                            "#37A2DA",
                                        ];
                                        return colorList[params.dataIndex];
                                    }
                                }
                            },
                            //柱状图上显示数据
                            label: {
                                show: "true",
                                // position: "top",
                                // color: "#FFF",
                                // fontWeight: "bolder",
                                // backgroundColor: "auto",
                                // fontSize: "16"
                            },
                            //平均值
                            markLine: {
                                data: [{ type: "average", name: "平均值" }]
                            },
                            markPoint: {
                                data: [
                                    {type: 'max', name: '最大值'},
                                    //{type: 'min', name: '最小值', symbol: 'arrow'}
                                ]
                            }
                        }
                    ]
                };
                // 使用刚指定的配置项和数据显示图表。
                barChart.setOption(option);

                return false;
            });
            $("form[role=form]").submit();
        },
        url: function (){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'weblog/log/url' + location.search,
                    table: 'weblog_url',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {field: 'url', title: __('URL'), operate:false, cellStyle: Controller.api.formatTableUnit, formatter: Controller.api.paramsMatter},
                        {field: 'visits_count', title: __('访问次数'), operate:false, sortable: true},
                        {field: 'ip_count', title: __('独立IP数'), operate:false, sortable: true}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        ip: function (){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'weblog/log/ip' + location.search,
                    table: 'weblog_url',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {field: 'ip', title: __('Ip'), events: Table.api.events.ip, formatter: Table.api.formatter.search},
                        {field: 'visits_count', title: __('访问次数'), operate:false, sortable: true},
                        {field: 'weblogspider.name', title: __('所属蜘蛛'), operate:false}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        code: function (){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'weblog/log/code' + location.search,
                    table: 'weblog_code',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {field: 'code', title: __('Code'), sortable: true},
                        {field: 'visits_count', title: __('出现次数'), operate:false, sortable: true},
                        {field: 'weblogcode.describe', title: __('状态描述'), operate:false}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            //表格超出宽度鼠标悬停显示td内容
            paramsMatter: function (value, row, index) {
                var span = document.createElement("span");
                span.setAttribute("title", value);
                span.innerHTML = value;
                return span.outerHTML;
            },
            //td宽度以及内容超过宽度隐藏
            formatTableUnit: function (value, row, index) {
                return {
                    css: {
                        "white-space": "nowrap",
                        "text-overflow": "ellipsis",
                        "overflow": "hidden",
                        "max-width": "60px"
                    }
                }
            }
        }
    };
    return Controller;
});